$(document).ready(function() {
  $.get('/assets/search/menu-index-zh.json').done(function(indexs){
    $.get('/assets/search/menu-source-zh.json').done(function (products) {
      var lunrSearchEngine = lunr.init(indexs);
      $('#menu-search-input').bind('input', function (e) {
        var searchResults = lunrSearchEngine.search(e.target.value);
        if (searchResults.length === 0) { // no matches
          $('#menu-search-results').html(
            '<li>' +
            '<h3 class="empty">'+ SEARCH_EMPTY +'</h3>' +
            '</li>'
          );
          return
        }
        $('#menu-search-results').html(function () {
          var appendString = '';
          for (var i = 0; i < searchResults.length; i++) {
            var product = products[searchResults[i].ref];
            if (product === undefined) {
              continue;
            }
            appendString += '<li>';
            appendString += '  <a class="overlay-close thumbnail" href="' + product.url + '">';
            appendString += '    <div class="preview circle" style="background-image: url('+ product.preview +')"></div>';
            appendString += '    <strong>'+ product.title +'</strong>';
            appendString += '  </a>';
            appendString += '</li>';
          }
          return appendString;
        }());
      })
    });
  });
})
